package com.mutouren.modules.org.client.impl;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.mutouren.modules.org.client.OrgAuthority;
import com.mutouren.modules.org.client.config.FunctionConfig;

public class OrgAuthorityImpl implements OrgAuthority {
	
	private FunctionConfig functionConfig = FunctionConfig.getInstance();;
	
	public OrgAuthorityImpl() {
	}

	@Override
	public boolean permit(HttpServletRequest request, int userId) {
		String actionName = request.getRequestURI().replace(request.getContextPath(),"");
		
		Map<String, Integer> mapUrl = functionConfig.getMapUrlOfApp();
		int[] userFunctionIds = functionConfig.getFunctionIdsOfUser(userId);
		
		Integer functionId = mapUrl.get(actionName);
		if (functionId == null) {
			return true;
		} else {
			for(int id : userFunctionIds) {
				if (functionId == id) {
					return true;
				}
			}
			return false;	
		}
	}

}
